import React, { Component } from 'react';
import { connect } from 'dva';
import { Modal, Form, Input, Radio, Spin, message } from 'antd';

const FormItem = Form.Item;
    @connect(state => ({
        ${classObjectName}: state.${classObjectName}
    }))
@Form.create()
export default class ${className}EditForm extends Component{
    /**
    *  提交表单数据
    */
    submitHandler = () => {
        this.props.form.validateFields((err, values) => {
            if (!err) {
                const { dispatch } = this.props;
                dispatch({
                    type: '${classObjectName}/edit',
                    payload: {values},
                    callback:(response) => {
                        dispatch({
                            type: '${classObjectName}/handleEditModalVisible',
                            payload: {visible:false}
                        });
                    }
                });
            }
        });
    };
    /**
    * 清除表单数据
    */
    handleFormClear = () =>{
        const { form } = this.props;
        form.resetFields();
    };

    handleModalVisible = (visible)=>{
        const { dispatch } = this.props;
        dispatch({
            type: '${classObjectName}/handleEditModalVisible',
            payload: {visible:visible}
        });
    };

    render(){
        const { ${classObjectName}:{ editModalVisible, record, loading },form} = this.props;
        const { getFieldDecorator } = form;
        const formItemLayout = {
            labelCol: {
                xs: { span: 10 },
                sm: { span: 5 }
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 14 }
            }
        };
        return (
            <span>
                <Modal visible={editModalVisible} title="创建表单" okText="提交" confirmLoading={loading} onCancel={()=>this.handleModalVisible(false)} afterClose={this.handleFormClear} onOk={this.submitHandler}>
                      <Spin tip="数据加载中..." spinning={loading}>
                        <Form layout="vertical">
                             <FormItem {...formItemLayout} >
                               {getFieldDecorator('id', {
                                 initialValue:record.id,
                                 rules: [{ required: true, message: 'id'}],
                               })(
                                 <Input type={'hidden'}/>
                               )}
                              </FormItem>
                        <#list fields as field>
                            <#if field.fieldName != keyName>
                            <FormItem {...formItemLayout} label="${field.remarks}" hasFeedback>
                              {getFieldDecorator('${field.fieldName}', {initialValue:record.${field.fieldName},
                                rules: [{ required: true, message: '请输入${field.remarks}！'}],
                              })(<Input />)}
                            </FormItem>
                            </#if>
                        </#list>
                        </Form>
                      </Spin>
                </Modal>
              </span>
         );
    }
}